local P = ClassFromFile(...)



P.waitingTips = list:new()

function P.GetTipPanelInfo()
    if not P.tipPanelInfo then
        local uiRoot = RootWindowManager.instance.uiRoot
        local tipPanel = uiRoot:GetComponentInChildren(typeof(TipPanel))
        local tipPanelInfo = GetComponentNew(tipPanel, TipPanelInfo)
        P.tipPanelInfo = tipPanelInfo
    end
    return P.tipPanelInfo
end

function P.Show(tipPrefab, showTime, fadeTime, remainParent)
    if fadeTime == nil then
        fadeTime = P.GetTipPanelInfo().fadeTime
    end

    if showTime == nil then
        showTime = P.GetTipPanelInfo().showTime
    end

    if tipPrefab == nil then
        tipPrefab = P.GetTipPanelInfo().tipPrefab
    end

    local tipObj = tipPrefab
    if not remainParent then
        tipObj = InstantiateNew(tipPrefab);
        tipObj.transform:SetParent(P.tipPanelInfo.transform, false);
    end

    local tip = GetComponentNew(tipObj, Tip)

    if remainParent or not P.waitEndTime or Time.time >= P.waitEndTime then
        P.RealShow(tip, showTime, fadeTime)
    else
        tip.gameObject:SetActive(false)
        local waitData = {}
        waitData.tip = tip
        waitData.showTime = showTime
        waitData.fadeTime = fadeTime
        P.waitingTips:unshift(waitData)
        if not P.coroutine then
            P.coroutine = coroutine.start(P.WaitHide)
            -- coroutine.resume(P.coroutine)
        end
    end

    return tip;
end

function P.GetClass(typeName)
	local typeScript = P[typeName]
	if not typeScript then
		typeScript = require("View/"..tostring(typeName).."Ctrl")
		P[typeName] = typeScript
	end

	return typeScript
end

function P.WaitHide()
    while P.waitEndTime or Time.time < P.waitEndTime do
        coroutine.wait(P.waitEndTime - Time.time)
        P.waitEndTime = 0

        if P.waitingTips.length > 0 then
            local waitData = P.waitingTips:pop()
            waitData.tip.gameObject:SetActive(false)
            P.RealShow(waitData.tip, waitData.showTime, waitData.fadeTime)
        else
            break
        end
    end

    P.coroutine = nil
end

function P.RealShow(tip, showTime, fadeTime)
    P.waitEndTime = Time.time + showTime + 2 * fadeTime

    local typeName = GetTypeName(tip)
    local classScript = P.GetClass(typeName)
    classScript.Show(tip, showTime, fadeTime)
end

function P.clean()
    if P.waitingTips.length > 0 then
        P.waitingTips.length = 0
    end
end